package cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.smbms.dao.user.BillMapper;
import cn.smbms.dao.user.ProviderMapper;
import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Page;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.util.SpringUtil;

/**
 * Servlet implementation class ServletMain
 */
@WebServlet("/ServletMain")
public class ServletMain extends HttpServlet {
	// private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletMain() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String path1 = request.getServletPath();
		String path = path1.substring(1, path1.length() - 3);
		System.out.println("当前路径是:" + path);
		if (path.equals("servlet") || path.equals("doPost")
				|| path.equals("doGet")) {
			response.sendError(500, "错误地址");
			return;
		}
		try {
			Method m = this.getClass().getDeclaredMethod(path,
					HttpServletRequest.class, HttpServletResponse.class);
			m.invoke(this, request, response);
		} catch (Exception e) {
			response.sendError(500, "错误地址");
			e.printStackTrace();
		}

	}// dopost
		// LoginServlet

	UserMapper um = (UserMapper) SpringUtil.getBean("UserMapper");
	BillMapper bm = (BillMapper) SpringUtil.getBean("BillMapper");
	ProviderMapper pm = (ProviderMapper) SpringUtil.getBean("ProviderMapper");

	protected void LoginServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		String userName = request.getParameter("username");
		String password = request.getParameter("password");

		/*
		 * UserMapperService umt = new UserMapperService(); List<User> userList
		 * = umt.getUserList();
		 */
		List<User> userList = um.getUserList();
		for (User uu : userList) {

			if (userName.equals(uu.getUserCode())
					&& password.equals(uu.getUserPassword())) {
				request.getSession().setAttribute("nowUser", uu);
				System.out.println("当前登录用户ID：" + uu.getId());
				request.setAttribute("user", uu);
				request.getRequestDispatcher("welcome.jsp").forward(request,
						response);
				return;
			}

		}// for
		PrintWriter out = response.getWriter();
		out.print("<script>alert('账号或密码错误');location='login.jsp';</script>");
		out.flush();

	}// LoginServlet

	// AddBillServlet

	protected void AddBillServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}
		String billCode = request.getParameter("billCode");

		String productName = request.getParameter("productName");

		String productUnit = request.getParameter("productUnit");

		int productCount = Integer
				.valueOf(request.getParameter("productCount"));

		double totalPrice = Double.parseDouble(request
				.getParameter("totalPrice"));

		long providerId = Long.valueOf(request.getParameter("supplier"));

		int isPayment = Integer.valueOf(request.getParameter("zhifu"));

		User nowUser = (User) request.getSession().getAttribute("nowUser");
		long createdBy = nowUser.getId();

		Bill pp = new Bill();
		pp.setBillCode(billCode);
		pp.setProductName(productName);
		pp.setProductUnit(productUnit);
		pp.setProductCount(productCount);
		pp.setTotalPrice(totalPrice);
		pp.setProviderId(providerId);
		pp.setIsPayment(isPayment);
		pp.setCreatedBy(createdBy);
		/*
		 * UserMapperService umt = new UserMapperService();
		 */
		int i = bm.addBill(pp);

		if (i > 0) {
			out.print("<script>alert('添加成功');location='SearchBillServlet.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('添加失败');location='SearchBillServlet.do?pageNo=1';</script>");

		}

		// out.flush();
		out.close();

	}

	// AddProvider
	public void AddProvider(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}
		String proCode = request.getParameter("providerId");
		String proName = request.getParameter("providerName");
		String proContact = request.getParameter("people");
		String proPhone = request.getParameter("phone");
		String proAddress = request.getParameter("address");
		String proFax = request.getParameter("fax");
		String proDesc = request.getParameter("describe");
		User nowUser = (User) request.getSession().getAttribute("nowUser");
		long createdBy = nowUser.getId();

		Provider pp = new Provider();
		pp.setProCode(proCode);
		pp.setProName(proName);
		pp.setProContact(proContact);
		pp.setProPhone(proPhone);
		pp.setProAddress(proAddress);
		pp.setProFax(proFax);
		pp.setProDesc(proDesc);
		pp.setCreatedBy(createdBy);
		/*
		 * UserMapperService umt = new UserMapperService();
		 */int i = pm.addProvider(pp);

		if (i > 0) {
			out.print("<script>alert('添加成功');location='SimilarSearchPV.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('添加失败');location='SimilarSearchPV.do?pageNo=1';</script>");

		}

		// out.flush();
		out.close();
	}// AddProvider

	// AddUserServlet
	protected void AddUserServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}
		String userCode = request.getParameter("userCode");

		String userName = request.getParameter("userName");

		String userPassword = request.getParameter("userPassword");

		int gender = Integer.valueOf(request.getParameter("gender"));

		Date birthday = Date.valueOf(request.getParameter("date"));

		String phone = request.getParameter("phone");

		String address = request.getParameter("address");

		int userRole = Integer.valueOf(request.getParameter("userRole"));

		User nowUser = (User) request.getSession().getAttribute("nowUser");
		long createdBy = nowUser.getId();

		User pp = new User();
		pp.setUserCode(userCode);
		pp.setUserName(userName);
		pp.setUserPassword(userPassword);
		pp.setGender(gender);
		pp.setBirthday(birthday);
		pp.setPhone(phone);
		pp.setAddress(address);
		pp.setUserRole(userRole);
		pp.setCreatedBy(createdBy);
		/*
		 * UserMapperService umt = new UserMapperService();
		 */
		int i = um.addUser(pp);

		if (i > 0) {
			out.print("<script>alert('添加成功');location='SimilarSearchUser.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('添加失败');location='SimilarSearchUser.do?pageNo=1';</script>");

		}

		// out.flush();
		out.close();

	}// AddUserServlet

	// BillDelete
	protected void BillDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}

		int id = Integer.parseInt(request.getParameter("pro"));
		/*
		 * UserMapperService umt = new UserMapperService();
		 */
		int i = bm.deleteBill(id);
		if (i > 0) {
			out.print("<script>alert('删除成功');location='SearchBillServlet.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('删除失败');location='SearchBillServlet.do?pageNo=1';</script>");

		}
		;
		out.close();

	}// BillDelete

	// BillServlet
	public void BillServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		/*
		 * UserMapperService umt = new UserMapperService();
		 */List<Bill> billList = bm.getBillList();
		List<Provider> providerList = pm.getProviderList();
		/*
		 * for (Bill pp : billList) {
		 * 
		 * System.out.println(pp.getProductName()); }
		 */
		request.setAttribute("providerList", providerList);

		request.setAttribute("billList", billList);
		request.getRequestDispatcher("billList.jsp").forward(request, response);
	}// BillServlet
		// BillUpdateServlet

	protected void BillUpdateServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		long id = Long.parseLong(request.getParameter("id"));
		String billCode = request.getParameter("billCode");
		String productName = request.getParameter("productName");
		String productUnit = request.getParameter("productUnit");
		int productCount = Integer.parseInt(request
				.getParameter("productCount"));
		double totalPrice = Double.parseDouble(request
				.getParameter("totalPrice"));
		long providerId = Long.parseLong(request.getParameter("providerId"));

		User nowUser = (User) request.getSession().getAttribute("nowUser");
		long modifyBy = nowUser.getId();
		String isPay = request.getParameter("zhifu");

		Bill bb = new Bill();
		bb.setId(id);
		bb.setProviderId(providerId);
		bb.setBillCode(billCode);
		bb.setProductName(productName);
		bb.setProductUnit(productUnit);
		bb.setProductCount(productCount);
		bb.setTotalPrice(totalPrice);
		bb.setModifyBy(modifyBy);
		System.out.println("付款状态是：" + isPay);
		if (isPay.equals("未付款")) {
			bb.setIsPayment(1);

		} else {
			bb.setIsPayment(2);

		}

		/*
		 * UserMapperService ums = new UserMapperService();
		 */int i = bm.updateBill(bb);

		if (i > 0) {
			out.print("<script>alert('修改成功');location='SearchBillServlet.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('修改失败');location='SearchBillServlet.do?pageNo=1';</script>");

		}
		out.close();
	}// BillUpdateServlet

	// BillViewServlet
	public void BillViewServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}

		long id = Integer.parseInt(request.getParameter("pro"));
		String vs = request.getParameter("vs");
		/*
		 * UserMapperService umt = new UserMapperService();
		 */// System.out.println(id);
		Bill pp = bm.getOneBill(id);
		request.setAttribute("pp", pp);
		if (vs.equals("view")) {
			request.getRequestDispatcher("billView.jsp").forward(request,
					response);
		}
		if (vs.equals("update")) {
			request.getRequestDispatcher("billUpdate.jsp").forward(request,
					response);
		}
		out.close();

	}// BillViewServlet

	// ChangePassword
	protected void ChangePassword(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}

		User uu = (User) request.getSession().getAttribute("nowUser");
		String npw = request.getParameter("newPassword");
		uu.setUserPassword(npw);
		uu.setModifyBy(uu.getId());
		/*
		 * UserMapperService umt = new UserMapperService();
		 */int i = um.changePassword(uu);

		if (i > 0) {
			out.print("<script>alert('修改成功');location='welcome.jsp';</script>");
		} else {
			out.print("<script>alert('修改失败');location='welcome.jsp';</script>");

		}
		out.close();

	}// ChangePassword

	// CheckProvider
	protected void CheckProvider(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		String proName = request.getParameter("name");

		/*
		 * UserMapperService umt = new UserMapperService();
		 */long id = pm.CheckProvider(proName);
		if (id > 0) {
			out.print(id);
		} else {
			out.print(-1);

		}

		out.close();
	}// CheckProvider

	// CheckUserCode
	protected void CheckUserCode(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		String uCode = request.getParameter("ucode");

		/*
		 * UserMapperService ums = new UserMapperService();
		 */List<User> userList = um.getUserList();
		int i = 0;
		for (User uu : userList) {

			if (uCode.equals(uu.getUserCode())) {
				i = 1;
				break;
			}
			;
		}
		out.print(i);
		out.close();

	}// CheckUserCode

	// Logout
	protected void Logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getSession().removeAttribute("nowUser");

		response.sendRedirect("login.jsp");

	}// Logout

	// OneUserServlet
	protected void OneUserServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		long id = Integer.parseInt(request.getParameter("pro"));
		String vs = request.getParameter("vs");
		/*
		 * UserMapperService umt = new UserMapperService();
		 */System.out.println("要查看的用户ID:" + id);
		User pp = um.getOneUser(id);
		if (vs.equals("show")) {
			request.setAttribute("pp", pp);
			request.getRequestDispatcher("userView.jsp").forward(request,
					response);
		}
		if (vs.equals("update")) {
			request.setAttribute("pp", pp);
			request.getRequestDispatcher("userUpdate.jsp").forward(request,
					response);
		}

	}// OneUserServlet

	// ProviderDelete
	protected void ProviderDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}

		int id = Integer.parseInt(request.getParameter("pro"));
		/*
		 * UserMapperService umt = new UserMapperService();
		 */
		int i = pm.deleteProvider(id);
		if (i > 0) {
			out.print("<script>alert('删除成功');location='SimilarSearchPV.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('删除失败');location='SimilarSearchPV.do?pageNo=1';</script>");

		}
		;
		out.close();

	}// ProviderDelete
		// ProviderUpdateServlet

	protected void ProviderUpdateServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		long id = Long.parseLong(request.getParameter("providerId"));
		String proCode = request.getParameter("providerCode");
		String proName = request.getParameter("providerName");
		String proContact = request.getParameter("people");
		String proPhone = request.getParameter("phone");
		String proAddress = request.getParameter("address");
		String proFax = request.getParameter("fax");
		String proDesc = request.getParameter("describe");
		User nowUser = (User) request.getSession().getAttribute("nowUser");
		long modifyBy = nowUser.getId();

		Provider pp = new Provider();
		pp.setId(id);
		pp.setProCode(proCode);
		pp.setProName(proName);
		pp.setProContact(proContact);
		pp.setProPhone(proPhone);
		pp.setProAddress(proAddress);
		pp.setProFax(proFax);
		pp.setProDesc(proDesc);
		pp.setModifyBy(modifyBy);
		/*
		 * UserMapperService umt = new UserMapperService();
		 */int i = pm.updateProvider(pp);

		if (i > 0) {
			out.print("<script>alert('修改成功');location='SimilarSearchPV.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('修改失败');location='SimilarSearchPV.do?pageNo=1';</script>");

		}

	}// ProviderUpdateServlet

	// ProviderViewServlet
	public void ProviderViewServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}

		int id = Integer.parseInt(request.getParameter("pro"));
		String vs = request.getParameter("vs");

		/*
		 * UserMapperService umt = new UserMapperService();
		 */Provider pp = pm.getOneProvider(id);

		request.setAttribute("pp", pp);
		if (vs.equals("show")) {
			request.getRequestDispatcher("providerView.jsp").forward(request,
					response);
		}
		;
		if (vs.equals("update")) {
			request.getRequestDispatcher("providerUpdate.jsp").forward(request,
					response);
		}
		;
		out.close();
	}// ProviderViewServlet

	// SearchBillServlet
	protected void SearchBillServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		String productName = null;
		Long providerId = 0L;
		Integer isPayment = null;
		productName = request.getParameter("SProdN");
		int pageNo = Integer.valueOf(request.getParameter("pageNo"));

		try {
			providerId = Long.valueOf(request.getParameter("providerId"));

		} catch (Exception e) {
			// TODO: handle exception
		}
		try {

			isPayment = Integer.valueOf(request.getParameter("isPayment"));

		} catch (Exception e) {
			// TODO: handle exception
		}
		System.out.println(productName + "," + providerId + "," + isPayment);
		/*
		 * UserMapperService umt = new UserMapperService();
		 */// Provider pp = umt.searchBillList(productName, providerId,
			// isPayment);
		List<Bill> billList = bm.searchBillList(productName, providerId,
				isPayment, pageNo, 8);

		List<Bill> billListA = bm.searchBillList(productName, providerId,
				isPayment, pageNo, 0);
		Page<Object> pp = new Page<Object>();
		int lines = billListA.size();
		int pNums = (lines % 8 == 0 ? (lines / 8) : (lines / 8 + 1));
		pp.setPageNums(pNums);
		pp.setPageNo(pageNo);
		List<Provider> providerList = pm.getProviderList();
		request.setAttribute("productName", productName);
		request.setAttribute("providerId", providerId);
		request.setAttribute("isPayment", isPayment);
		request.setAttribute("pp", pp);
		request.setAttribute("providerList", providerList);
		request.setAttribute("billList", billList);
		request.getRequestDispatcher("billList.jsp").forward(request, response);

	}// SearchBillServlet

	// ShowProviderServlet
	protected void ShowProviderServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		/*
		 * UserMapperService umt = new UserMapperService();
		 */List<Provider> proList = pm.getProviderList();
		String vs = request.getParameter("vs");
		/*
		 * for (Provider pp : proList) { System.out.println(pp.getProContact());
		 * }
		 */

		request.setAttribute("proList", proList);

		if (vs.equals("show")) {
			request.getRequestDispatcher("providerList.jsp").forward(request,
					response);
		}
		if (vs.equals("get")) {
			request.getRequestDispatcher("billAdd.jsp").forward(request,
					response);
		}

	}// ShowProviderServlet

	// SimilarSearchPV
	protected void SimilarSearchPV(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String similar = request.getParameter("searchPVN");
		/*
		 * UserMapperService ums = new UserMapperService();
		 */int pageNo = Integer.valueOf(request.getParameter("pageNo"));

		List<Provider> proList = pm.searchProviderList(similar, pageNo, 8);
		List<Provider> proListA = pm.searchProviderList(similar, pageNo, 0);

		Page<Object> pp = new Page<Object>();
		int lines = proListA.size();
		// System.out.println("供应商条数是：" + lines);
		int pNums = (lines % 8 == 0 ? (lines / 8) : (lines / 8 + 1));
		pp.setPageNums(pNums);
		pp.setPageNo(pageNo);
		request.setAttribute("similar", similar);
		request.setAttribute("pp", pp);
		request.setAttribute("proList", proList);
		request.getRequestDispatcher("providerList.jsp").forward(request,
				response);

	}// SimilarSearchPV

	// SimilarSearchUser
	protected void SimilarSearchUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String similar = request.getParameter("userNameS");
		/*
		 * UserMapperService ums = new UserMapperService();
		 */int pageNo = Integer.valueOf(request.getParameter("pageNo"));

		List<User> userList = um.similarSearchUser(similar, pageNo, 8);
		List<User> userListA = um.similarSearchUser(similar, pageNo, 0);
		Page<Object> pp = new Page<Object>();
		int lines = userListA.size();
		int pNums = (lines % 8 == 0 ? (lines / 8) : (lines / 8 + 1));
		for (User uu : userList) {

			try {
				uu.setAge(getAge(uu.getBirthday()));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pp.setPageNums(pNums);
		pp.setPageNo(pageNo);
		request.setAttribute("similar", similar);
		request.setAttribute("pp", pp);
		request.setAttribute("userList", userList);
		request.getRequestDispatcher("userList.jsp").forward(request, response);

	}// SimilarSearchUser

	// UserDeleteServlet
	protected void UserDeleteServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}

		int id = Integer.parseInt(request.getParameter("pro"));
		/*
		 * UserMapperService umt = new UserMapperService();
		 */
		int i = um.deleteUser(id);
		if (i > 0) {
			out.print("<script>alert('删除成功');location='SimilarSearchUser.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('删除失败');location='SimilarSearchUser.do?pageNo=1';</script>");

		}
		;
		out.close();

	}// UserDeleteServlet

	// UserUpdateServlet
	protected void UserUpdateServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		if (request.getSession().getAttribute("nowUser") == null) {
			out.print("<script language='javascript'>alert('对不起，您还没有登陆,请登陆!');window.location='login.jsp';</script>");
			return;
		}

		long id = Long.parseLong(request.getParameter("id"));

		String userName = request.getParameter("userName");
		int gender = Integer.valueOf(request.getParameter("gender"));

		java.sql.Date birthday = java.sql.Date.valueOf(request
				.getParameter("birthday"));

		String phone = request.getParameter("phone");
		String address = request.getParameter("address");

		int userRole = Integer.parseInt(request.getParameter("userRole"));

		User nowUser = (User) request.getSession().getAttribute("nowUser");
		long modifyBy = nowUser.getId();

		User bb = new User();

		bb.setId(id);
		bb.setUserName(userName);
		bb.setGender(gender);
		bb.setBirthday(birthday);
		bb.setPhone(phone);
		bb.setAddress(address);
		bb.setUserRole(userRole);
		bb.setModifyBy(modifyBy);

		/*
		 * UserMapperService ums = new UserMapperService();
		 */int i = um.updateUser(bb);

		if (i > 0) {
			out.print("<script>alert('修改成功');location='SimilarSearchUser.do?pageNo=1';</script>");
		} else {
			out.print("<script>alert('修改失败');location='SimilarSearchUser.do?pageNo=1';</script>");

		}
		out.close();
	}// UserUpdateServlet

	// UserViewServlet

	public int getAge(Date birthDay) throws Exception {
		Calendar cal = Calendar.getInstance();

		if (cal.before(birthDay)) {
			throw new IllegalArgumentException(
					"The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);

		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth)
					age--;
			} else {
				age--;
			}
		}
		return age;
	}

	protected void UserViewServlet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		/*
		 * UserMapperService umt = new UserMapperService();
		 */List<User> userList = um.getUserList();
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");

		for (User pp : userList) {

			try {
				pp.setAge(getAge(pp.getBirthday()));
				System.out.println(pp.getRoleName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		request.setAttribute("userList", userList);
		request.getRequestDispatcher("userList.jsp").forward(request, response);
	}// UserViewServlet

}// httpservlet
